package com.tgy.ailiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.tgy.ailiyun.dao.UserDao;
import com.tgy.ailiyun.entity.User;
import com.tgy.ailiyun.entity.UserLoginInf;
import com.tgy.ailiyun.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-08-28 21:55:57
 */
@Controller
@Slf4j
public class UserController {
//    static User user;

    @Resource
    UserDao userDao;
    @Resource
    UserServiceImpl userService;


    @RequestMapping("/hi/flag")
    private String flag(){
        return "flag";
    }

    @RequestMapping("/")
    public String list(Model model){
        return "index";
    }



    @RequestMapping("login.do")
    @ResponseBody
    public HashMap<String, Object> login(@RequestBody User user){
        HashMap<String, Object> map =new HashMap<>();
        map.put("code",111);
        if(new LambdaQueryChainWrapper<>(userDao)
                .eq(User::getLogin, user.getLogin())
                .list().size()==0)
        {
            return map;
        }
        try {
            String md5In = GetMD5(user.getPassword());

/*            System.out.println(userDao.selectList(new QueryWrapper<User>().select("md5").eq("login", user.getLogin())));
            System.out.println(userDao.selectList(new QueryWrapper<User>().lambda().select(User::getMd5).eq(User::getLogin, "999")));
            String md5Out = this.userDao.getMD5(user.getLogin());*/
            //简化方法
            //String md5Out = userDao.selectList(new QueryWrapper<User>().lambda().select(User::getMd5).eq(User::getLogin,user.getLogin())).get(0).getMd5();
            String md5Out = userDao.selectList(new QueryWrapper<User>().lambda().eq(User::getLogin,user.getLogin())).get(0).getMd5();
            String md5 = (new LambdaQueryChainWrapper<>(userDao).eq(User::getLogin, user.getLogin()).list()).get(0).getMd5();

            //TODO 更安全的比较两个String
            if(Objects.equals(md5In,md5Out)){
                map.put("code",999);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }finally {
         return map;
        }
    }

    @RequestMapping("addUser")
    @ResponseBody
    public Integer addStudent(@RequestBody User user){
        user.setMd5(GetMD5(user.getLogin()));
        return this.userService.add(user);

    }


    @RequestMapping("ddd")
    public String showIndex(){
        return "indexaa";
    }

    @PostMapping(value = "/GetMD5.do")
    @ResponseBody
    public String PostMD5(String value) {
        try{
           return GetMD5(value);
//            return GetMD5(GetMD5(GetMD5(value)));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static String GetMD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
